package com.yunsom.testdemo.collection;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author Xbao
 * @version 1.0
 * @description
 * @date 2021/3/22 10:44
 */
public class ListTest {

  public static void main(String[] args) {
    List<String> list1 = new ArrayList<>();
    list1.add("1");
    list1.add("1");
    list1.add("2");
    list1.add("3");

    Set<String> list2 = new HashSet<>();
    list2.add("1");

    System.out.println(list1);
    list1.retainAll(list2);
    System.out.println(list1);

    System.out.println("list1 contains list2 " + list1.containsAll(list2));

    list1.removeAll(list2);
    System.out.println(list1);
    System.out.println(list2);

    List<String> ids1 = new ArrayList<>();
    ids1.add("1");
    ids1.add("2");
    ids1.add("3");
    ids1.add("4");
    ids1.add("6");
    ids1.add("7");
    List<String> ids2 = new ArrayList<>();
    ids2.add("4");
    ids2.add("6");
    ids2.add("7");
    System.out.println(ids1.containsAll(ids2));

    for (String s : ids1) {
      for (String s1 : ids2) {
        System.out.println("1111");
        break;
      }
    }

    // 支持迭代过程中删除元素的集合
    CopyOnWriteArrayList<String> copyOnWriteArrayList = new CopyOnWriteArrayList<>();
    copyOnWriteArrayList.add("4");
    copyOnWriteArrayList.add("63");
    copyOnWriteArrayList.add("62");
    copyOnWriteArrayList.add("6");
    copyOnWriteArrayList.add("7");
    System.out.println(copyOnWriteArrayList.toString());
    for (String s : copyOnWriteArrayList) {
      if (s.contains("2")){
        copyOnWriteArrayList.remove(s);
      }
      System.out.println(s);
    }
    System.out.println(copyOnWriteArrayList.toString());

  }
}
